/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import javax.ejb.Stateless;
import entities.Event;
import entities.EventType;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import persistance.dao.CasusDAO;
import persistance.model.*;

/**
 *
 * @author Bobok
 */
@Singleton
public class FromCasusEventDAO implements EventDAOLocal {

    private CasusDAO casusDAO;

    @EJB
    private CasusDAOLocal casusDAOLocal;
    private CasusDAOFatory casusDAOFactory = new CasusDAOFatory(){

        public CasusDAO getCasusDAO() {
            return casusDAOLocal;
        }

    };

    public CasusDAO getCasusDAO() {
        if (casusDAO == null){
            casusDAO = casusDAOFactory.getCasusDAO();
        }
        return casusDAO;
    }

    public void setCasusDAO(CasusDAO casusDAO) {
        this.casusDAO = casusDAO;
    }

    public FromCasusEventDAO() {
        casusDAO = casusDAOFactory.getCasusDAO();
    }

    public FromCasusEventDAO(CasusDAO casusDAO) {
        this.casusDAO = casusDAO;
    }

    /**
     * Pobiera wszystkie eventy z bazy, bez wzgledu na ich głebokość, położenie , czas trwania itp.
     * @deprecated
     * @warning tylko we wczesnej fazie projektu!
     * @return
     */
    public List<Event> getAllEvents() {
        List<Casus> casuses = getCasusDAO().getAllCasuses();
        List<Event> events = new ArrayList<Event>();
        for(Casus casus : casuses){
            events.add(this.toEvent(casus));
        }
        return events;
    }

    public void insertEvent(Event event) {
        getCasusDAO().insertCasus(this.toCasus(event));
    }

    public void updateEvent(Event event) {
        getCasusDAO().updateCasus(this.toCasus(event));
    }

    public void deleteEvent(long id) {
        getCasusDAO().deleteCasus(id);
    }

    private Event toEvent(Casus casus){
        Long id = casus.getId();
        String title = casus.getTitle();
        double lat = casus.getTimeAndPlace().getCenterLat();
        double lng = casus.getTimeAndPlace().getCenterLng();
        int year = casus.getTimeAndPlace().getYear();
        int day = casus.getTimeAndPlace().getDay();
        Signum sign = casus.getMarker();
        EventType eventType = new EventType(sign.getName(), sign.getIconUrl());
        Event event = new Event(id, title, lat, lng, year, day, eventType);
        event.setDescription(casus.getDescription());
        return event;
    }

    private Casus toCasus(Event event){
        Casus casus = new Casus();
        casus.setId(event.getId());
        casus.setTitle(event.getTitle());
        casus.setDescription(event.getDescription());
        TempusLocus tl = new TempusLocus();
        tl.setCenterLat(event.getLat());
        tl.setCenterLng(event.getLng());
        tl.setYear(event.getYear());
        tl.setDay(event.getDay());
        casus.setTimeAndPlace(tl);
        Signum sign = new Signum();
        sign.setName(event.getType().getName());
        sign.setIconUrl(event.getType().getIconUrl());
        casus.setMarker(sign);
        return casus;
    }
}
